package org.naigu.peach.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.naigu.peach.biz.CommentBiz;
import org.naigu.peach.dal.dao.CommentDAO;
import org.naigu.peach.dal.dataobject.CommentDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class CommentBizImpl implements CommentBiz{

    private final static Logger logger = LoggerFactory.getLogger(CommentBizImpl.class);

    @Autowired
    private CommentDAO commentDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            CommentDO commentDO = commentDAO.getCommentById(id);
            bizResult.data.put("commentDO", commentDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Comment error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = commentDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<CommentDO> commentList = commentDAO.selectForPage(baseQuery);
            bizResult.data.put("commentList", commentList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Comment list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            commentDAO.delCommentById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete comment error", e);
        }
        return bizResult;
    }

    public BizResult create(CommentDO commentDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = commentDAO.insert(commentDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Comment error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(CommentDO commentDO) {
        BizResult bizResult = new BizResult();
        try {
            commentDAO.updateByIdSelective(commentDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Comment error", e);
        }
        return bizResult;
    }


}
